package com.ganyu.component;


import com.ganyu.mapper.ArticleMapper;
import com.ganyu.service.ArticleService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * DATA: 2022/9/27
 * Author: 晨夕
 * @Description 定时任务 每过多少时间会执行
 */
@Component
public class TimeTask {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ArticleMapper articleMapper;

    @Scheduled(cron = "0 0 12 * * ?")
    public void getFromRedis(){
        //修改数据库中的浏览量，数据为redis中的数据
        Map<String, Integer> cacheMap = redisCache.getCacheMap("article:viewCount");
        //将Map转为Set
        Set<Map.Entry<String, Integer>> entries = cacheMap.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
        while(iterator.hasNext()){
            Map.Entry<String, Integer> entry = iterator.next();
            articleMapper.UpdateViewCount(Long.parseLong(entry.getKey()), entry.getValue().longValue());
        }
    }
}
